<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<c:set var="currentMenu" value="${boardType}" />
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<title>JK Holdings</title>
<script src="/js/jquery-1.6.4.js"></script>
<link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
<script src="/js/jquery-ui-1.8.18.custom.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/jquery-ui-1.8.18.custom.css" media="screen" />
<script type="text/javascript">
var validationInfo = {
	validationInfoList : [
		{ inputId : 'boardReplyContent', 	errorMessage : '내용을 입력해주세요' 				},
		{ inputId : 'boardId', 				errorMessage : '시스템 오류입니다 F5번 키를 눌러주세요 ' 	},
		{ inputId : 'boardType', 			errorMessage : '시스템 오류입니다 F5번 키를 눌러주세요 ' 	}
	]
};
var submitCheckStatus = true;
$(document).ready(function(){
	$('#replySaveButton').click(function(){
		submitCheckStatus = true;
		$.each(validationInfo.validationInfoList, function(key, value) {
			var $tempInputObj = $('#'+value.inputId);
			if($tempInputObj.val().length < 1 ){
				alert(value.errorMessage);
				$tempInputObj.focus();
				submitCheckStatus = false;
				return false;
			}
		});
		
		if( submitCheckStatus == true ){
			$('#replyForm').submit();
			return false;
		}
	});	
	
	$("#deleteArticle").click(function() {
		
		$("#confirmDeleteDialog").dialog({
			title: "비밀번호를 입력해주세요.",
			width: 230,
			minHeight: 120,
			buttons: {
				"확인" : function() {
					var boardPassWord = $('#boardPassWord').val();
					var boardIdInput = $('#boardId').val();
					var boardTypeInput = $('#boardType').val();
					$.ajax({
					    type : 'POST',
					    url : '/board/checkPassWord.do',
					    data : 'boardPassWord=' + boardPassWord + '&boardId=' + boardIdInput + '&boardType=' + boardTypeInput,
					    dataType : 'text',
					    success : function(data){
					      if(data == 'true'){
					    	  $('#boardIdInput').val(boardIdInput);
					    	  $('#boardTypeInput').val(boardTypeInput);
					    	  $('#confirmDeleteForm').submit();
					      }else{
					    	  $("#wrongPassword").show();
					      }
					    }
					 });
					
				}, "취소": function() {
					$("#confirmDeleteDialog").dialog("close");
				}
			}
		});
	});
});

/**
 * @desc 댓글을 삭제한다. 
 * @param arg : 댓글 삭제 아이디
 */
function deleteReply(boardReplyId, boardId){
	var form = $('<form></form>');
	$(form).attr('action', "/boardReply/form/delete.do");
	$(form).attr('method', "POST");
	$(form).append('<input name="boardType" 	value="${boardType}"/>');
	$(form).append('<input name="boardId" 		value="' + boardId + '"/>');
	$(form).append('<input name="boardReplyId" 	value="' + boardReplyId + '"/>');
	$('body').append($(form));
	$(form).submit();
}
</script>
</head>
<body id="top">
	<!-- 공통 헤더 시작 -->
	<%@ include file="/WEB-INF/head.jsp"%>
	<!-- 공통 헤더 끝 -->
	<!-- 메인 네비게이션 시작  -->
	<%@ include file="/WEB-INF/navigation.jsp"%>
	<!-- 메인 네비게이션 끝  -->
	<div id="content-wrapper">
		<div class="center-wrapper">
			<div class="content" id="content-two-columns">
				<div id="main-wrapper">
					<div id="main">
						<div class="post">
							<div class="post-title">
								<h1>${board.boardTitle}</h1>
							</div>
							<div class="post-body">
								<table>
									<tr>
										<th>작성일</th>
										<th>${board.registrationDay}</th>
									</tr>
									<tr>
										<th>작성자</th>
										<th>${board.boardWriter}</th>
									</tr>
								</table>
								<p>${board.boardContent}</p>
								<input type="hidden" id="boardId" 	name="boardId" 		value="${board.boardId}" 	/>
								<input type="hidden" id="boardType" name="boardType" 	value="${board.boardType}" 	/>
							</div>
						</div>
						<button type="button" title="수정" class="write" onclick="location.href='/board/modifyForm.do?boardType=${board.boardType}&boardId=${board.boardId}'">
							<span>수정</span>
						</button>
						<button class="del_32" type="button" id="deleteArticle">
							<span>글 삭제</span>
						</button>
						<button type="button" title="리스트" class="list" onclick="location.href='/board/list.do?boardType=${board.boardType}'">
							<span>목록</span>
						</button>

						<!-- 일반 사용자도 보이는 댓글 영역 -->
						<c:if test="${boardReplyList != null && fn:length(boardReplyList) != 0 }">
							<div>
								<c:forEach var="boardReply" items="${boardReplyList}">
								<table style="border-width: 0px;">
									<tr>
										<th colspan="2">
											<!-- 관리자만 보이는 버튼 -->
											<h3>답글 &nbsp;&nbsp;<button id="boardReplyDeleteButton_${boardReply.boardReplyId}" onclick="deleteReply('${boardReply.boardReplyId}','${board.boardId}')" class="del" type="button"><span>덧글삭제</span></button></h3>
											<!-- 관리자만 보이는 버튼 -->
										</th>
									</tr>
									<tr>
										<td>
											<p>${boardReply.boardReplyContent}</p>
										</td>
									</tr>
								</table>
								</c:forEach>
							</div>
						</c:if>
						<!-- 일반 사용자도 보이는 댓글 영역 -->
						<!-- 관리자만 보이는 영역 시작 -->
						<c:if test='${session == null}'>
							<div>
								<table style="border-width: 0px;">
									<tr>
										<th colspan="2"><h3>답글 남기기</h3></th>
									</tr>
									<tr>
										<td>
											<form id="replyForm" action="/boardReply/form/add.do" method="post">
												<textarea name="boardReplyContent" id="boardReplyContent" style="width: 570px; height: 64px;"></textarea>
												<input type="hidden" id="boardIdForm" 	name="boardId" 		value="${board.boardId}" 	/>
												<input type="hidden" id="boardTypeForm" name="boardType" 	value="${board.boardType}" 	/>
											</form>
										</td>
										<td>
											<button id="replySaveButton" class="reply" title="댓글달기">
												<span>댓글달기</span>
											</button>
										</td>
									</tr>
								</table>
							</div>
						</c:if>
						<!-- 관리자만 보이는 영역 끝 -->
					</div>
				</div>

				<div id="sidebar-wrapper">
					<div id="sidebar">

						<!-- 카테고리 box 시작 -->
						<%@ include file="/WEB-INF/category.jsp"%>
						<!-- 카테고리 box 끝 -->

						<div class="box nobborder">

							<div class="box-title">About</div>

							<div class="box-content">
								<p>뭔가 About 설명이 들어가야할 영역</p>
							</div>

						</div>

					</div>
				</div>
				<div class="clearer">&nbsp;</div>
			</div>
		</div>
	</div>
	<div id="confirmDeleteDialog" style="display: none;">
		<form id="confirmDeleteForm" action="/board/form/delete.do" method="post">
			<div class="error" style="display: none;" id="wrongPassword">비밀번호가 틀렸습니다.</div>
			<input type="password" name="boardPassWord" id="boardPassWord"/>
			<input type="hidden" name="boardId" 	id="boardIdInput" />
			<input type="hidden" name="boardType" 	id="boardTypeInput" />
		</form>
	</div>
	<!-- bottom 시작 -->
	<%@ include file="/WEB-INF/bottom.jsp"%>
	<!-- bottom 끝 -->
</body>
</html>